package com.winit.need.web;

import com.alibaba.fastjson.JSONArray;
import com.winit.common.base.bean.ExecuteResult;
import com.winit.common.base.web.BaseController;
import com.winit.need.model.Need;
import com.winit.need.service.NeedService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;
import java.util.Map;
@Component
@Api(value = "need", tags = {"need"}, description = "need")
@RestController
@RequestMapping("/api/v1/need")
public class NeedController extends BaseController {

    private static Logger log = LoggerFactory.getLogger(NeedController.class);

    @Autowired
    private NeedService needService;

    @RequestMapping(value = "/getNeeds", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "根据needID查询详情")
    @ResponseBody
    public ExecuteResult<List<Need>> getNeed(@ApiParam(value = "根据id查询") @RequestParam(value = "id", required = false) String id,
                                             @ApiParam(value = "是否返回page信息") @RequestParam(value = "isPage", required = false) Boolean isPage,
                                             @ApiParam(value = "状态") @RequestParam(value = "status", required = false) String status,
                                             @ApiParam(value = "提出人") @RequestParam(value = "proposerAccount", required = false) String proposerAccount,
                                             @ApiParam(value = "需求来源") @RequestParam(value = "source", required = false) String source,
                                             @ApiParam(value = "关键字") @RequestParam(value = "keyword", required = false) String keyword,
                                             @ApiParam(value = "pdm负责人", required = false) @RequestParam(value = "pdmAccount", required = false) String pdmAccount,
                                             @ApiParam(value = "pdm分组", required = false) @RequestParam(value = "pdmGroup", required = false) String pdmGroup,
                                             @ApiParam(value = "开始时间", required = false) @RequestParam(value = "startDate", required = false) Long startDate,
                                             @ApiParam(value = "是否查看已上线", required = false) @RequestParam(value = "isOnline", required = false) Boolean isOnline,
                                             @ApiParam(value = "受理人", required = false) @RequestParam(value = "acceptAccount", required = false) String acceptAccount,
                                             @ApiParam(value = "指定受理人", required = false) @RequestParam(value = "appointAcceptAccount", required = false) String appointAcceptAccount,
                                             @ApiParam(value = "我的需求", required = false) @RequestParam(value = "myAccount", required = false) String myAccount
                                             ) {
        ExecuteResult<List<Need>> er = needService.getNeedList(id,isPage,status,proposerAccount,source,keyword,pdmAccount,pdmGroup,startDate,isOnline,acceptAccount,appointAcceptAccount,myAccount);

        return er;
    }

    @RequestMapping(value = "/saveNeed", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "创建need/或更新need")
    @ResponseBody
    public ExecuteResult<Need> saveNeed(@RequestBody  Need need){
        ExecuteResult<Need> executeResult = new ExecuteResult();
        if(needService.selectByPrimaryKey(need.getId())!=null){
            executeResult=needService.updateNeed(need);
        }else{
            executeResult=needService.saveNeed(need);
        }
        return executeResult;
    }

    @RequestMapping(value = "/getStatusCount", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "根据所给状态返回统计数据")
    @ResponseBody
    public ExecuteResult<Map<String, Long>> getStatusCount(
            @ApiParam(value = "根据所给状态返回统计数据") @RequestParam(value = "statusList", required = false) String statusList,
            @ApiParam(value = "开始时间", required = false) @RequestParam(value = "starDate", required = false) Long starDate,
            @ApiParam(value = "结束时间", required = false) @RequestParam(value = "endDate", required = false) Long endDate,
            @ApiParam(value = "提出人", required = false) @RequestParam(value = "proposerAccount", required = false) String proposerAccount,
            @ApiParam(value = "提出部门", required = false) @RequestParam(value = "proposerDepartment ", required = false) String proposerDepartment,
            @ApiParam(value = "产品组", required = false) @RequestParam(value = "productGroup ", required = false) String productGroup,
            @ApiParam(value = "pdm负责人", required = false) @RequestParam(value = "pdmAccount ", required = false) String pdmAccount,
            @ApiParam(value = "需求来源") @RequestParam(value = "source", required = false) String source
    ) {
        ExecuteResult<Map<String,Long>> er = needService.getStatusCount(JSONArray.parseArray(statusList),starDate,endDate,proposerAccount,proposerDepartment,productGroup,pdmAccount,source);
        return er;
    }

    @RequestMapping(value = "/updateNeedStar", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "修改评星等级")
    @ResponseBody
    public ExecuteResult<Map<String, Long>> updateNeedStar(
            @ApiParam(value = "需求id", required = true) @RequestParam(value = "id", required = false) String id,
            @ApiParam(value = "星级") @RequestParam(value = "star", required = false) String star
    ) {
        ExecuteResult er = needService.updateNeedStar(id, star);
        return er;
    }

    //每日更新未受理状态
    @Scheduled(cron="0 0 00 * * ?")
    public void timingUpdate() throws ParseException {
        needService.timingUpdate();
    }

    //每日更新已上线任务改为结束
    @Scheduled(cron="0 0 01 * * ?")
    public void timingUpdateClosed() throws ParseException {
        needService.timingUpdateClosed();
    }

    /**
     * 查询及时受理率与完成率
     * @param type
     * @param starDate
     * @param endDate
     * @param proposerAccount
     * @param proposerDepartment
     * @param productGroup
     * @param pdmAccount
     * @return
     */
    @RequestMapping(value = "/getAcceptanceProbability", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "查询及时受理率")
    @ResponseBody
    public ExecuteResult<Map<String,Long>> getAcceptanceProbability(
            @ApiParam(value = "受理率：ACCEPT，完成率：COMPLETE", required = true) @RequestParam(value = "type", required = false) String type,
            @ApiParam(value = "开始时间", required = false) @RequestParam(value = "starDate", required = false) Long starDate,
            @ApiParam(value = "结束时间", required = false) @RequestParam(value = "endDate", required = false) Long endDate,
            @ApiParam(value = "提出人", required = false) @RequestParam(value = "proposerAccount", required = false) String proposerAccount,
            @ApiParam(value = "提出部门", required = false) @RequestParam(value = "proposerDepartment ", required = false) String proposerDepartment,
            @ApiParam(value = "产品组", required = false) @RequestParam(value = "productGroup ", required = false) String productGroup,
            @ApiParam(value = "pdm负责人", required = false) @RequestParam(value = "pdmAccount ", required = false) String pdmAccount,
            @ApiParam(value = "需求来源") @RequestParam(value = "source", required = false) String source
    ){
        return needService.getAcceptanceProbability(type,starDate,endDate,proposerAccount,proposerDepartment,productGroup,pdmAccount,source);
    }


    /**
     * 根据needId查询plan信息
     * @param needId
     * @return
     */
    @RequestMapping(value = "/getNeedPlan", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "根据needId查询plan列表")
    @ResponseBody
    public ExecuteResult<List> getNeedPlan(
            @ApiParam(value = "needId", required = true) @RequestParam(value = "needId", required = false) String needId
           ){
        return needService.getNeedPlan(needId);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "物理删除需求")
    @ResponseBody
    public ExecuteResult deleteNeed(
            @ApiParam(value = "id", required = true) @RequestParam(value = "id ", required = false) String id){
        return needService.deleteNeed(id);
    }

    /**
     * 根据受理人与状态查询需求列表
     * @param acceptAccount
     * @param status
     * @return
     */
    @RequestMapping(value = "/getAcceptAccountAndStatus", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "根据受理人,状态查询需求列表")
    @ResponseBody
    public ExecuteResult<List> getAcceptAccountAndStatus(
            @ApiParam(value = "受理人", required = false) @RequestParam(value = "acceptAccount", required = false) String acceptAccount,
            @ApiParam(value = "状态", required = false) @RequestParam(value = "status", required = false) String status

    ){
        return needService.getAcceptAccountAndStatus( acceptAccount, status);
    }

}
